﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.ComponentModel.Design;
using System.Xml;
using UmcProvider.Shared;
using UmcProvider.PropertiesDesign;
using UmcControls.DesignInterface;

namespace Wicresoft.Shell.Package.AppExp.ToolboxControl
{
    [Designer(typeof(UmcDesigner<IUmcScript>))]
    [ToolboxItem(false)]
    [DesignTimeVisible(false)]
    public class Script : Control , IUmcPropertyStore
    {
        //store the properties parse from code which is not surpported by designer
        //also store the properties which is set value using script variable
        //users should operator the dic through the IUmcPropertyStore
        private Dictionary<string, string> DesignViewUnSurpportProperty = new Dictionary<string, string>();
        #region IUmcPropertyStore Members

        public void AddUmcProperty(string property, string value)
        {
            DesignViewUnSurpportProperty.Add(property, value);
        }

        public void RemoveUmcProperty(string property)
        {
            DesignViewUnSurpportProperty.Remove(property);
        }

        public void RemoveAllUmcProperties()
        {
            DesignViewUnSurpportProperty.Clear();
        }

        public string GetUmcProperty(string property)
        {
            if (DesignViewUnSurpportProperty.ContainsKey(property))
            {
                return DesignViewUnSurpportProperty[property];
            }
            else
            {
                return null;
            }
        }

        public List<KeyValuePair<string, string>> GetUmcPropertyList()
        {
            return DesignViewUnSurpportProperty.ToList();
        }

        #endregion
    }

    public sealed class UmcScript : DefaultConverter<UmcScript, Script>, IUmcScript
    {
        public override XmlNode[] Serialize(IComponent root, XmlDocument document)
        {
            Script spt = root as Script;
            XmlNode node = document.CreateNode(XmlNodeType.Element, "Script", null);
            if (spt.Tag != null)
            {
                node.InnerXml = spt.Tag.ToString();
            }
            else
            {
                node.InnerXml = "";
            }
            return new XmlNode[] { node };
        }

        public override IComponent DeSerialize(IDesignerHost host, XmlDocument document, XmlNode[] node)
        {
            IComponent component = host.CreateComponent(typeof(Script));
            (component as Script).Tag = node.First().OuterXml;
            return component;
        }

        private void SetTag(IComponent component,string text)
        {
            var prop = typeof(Script).GetProperty("Tag");
            if (prop != null)
            {
                prop.SetValue(component, text, null);
            }
        }


        #region IUmcScript Members

        public string Tag
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }


    public interface IUmcScript : IUmc
    {
        string Tag { get; }
    }
}
